﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http.Filters;
using ZLB_Unility.Enums;
using ZLB_Unility.Extendsions;
using ZLB_Unility.Log;
using ZLB_Unility.Results;

namespace ZLB_Report_WebApi.Filters
{
    public class GlobalExceptionAttribute : ExceptionFilterAttribute
    {
        private LogHelper logHelper = new LogHelper(typeof(GlobalExceptionAttribute));
        private const string Origin = "Origin";

        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            logHelper.Error(actionExecutedContext.Exception.Message, actionExecutedContext.Exception);
            HttpRequestMessage request = actionExecutedContext.Request;

            ResultInfo res = CodeStatus.error_500.GetStatus(actionExecutedContext.Exception.Message);


            actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(
               HttpStatusCode.InternalServerError, res);

            // 把当前请求的返回指定一下

            MediaTypeHeaderValue mediaTypeHeaderValue = new MediaTypeHeaderValue("application/json");
            mediaTypeHeaderValue.CharSet = "utf-8";
            actionExecutedContext.Response.Content.Headers.ContentType = mediaTypeHeaderValue;

            CommonFilterUtils.SetHttpInfo();

            base.OnException(actionExecutedContext);
        }

        public class CommonFilterUtils
        {
            private const string Origin = "Origin";

            public static void SetHttpInfo(HttpResponseMessage resp)
            {
                SetHttpInfo();
            }

            public static void SetHttpInfo()
            {
                var headers = HttpContext.Current.Request.Headers;
                var response = HttpContext.Current.Response;
                if (headers.AllKeys.Contains(Origin))
                {
                    string headerOrigin = HttpContext.Current.Request.Headers.GetValues(Origin).FirstOrDefault();
                    if (!string.IsNullOrWhiteSpace(headerOrigin))
                    {
                        if (headerOrigin.StartsWith("http://http://", StringComparison.InvariantCultureIgnoreCase) || headerOrigin.StartsWith("https://https://", StringComparison.InvariantCultureIgnoreCase))
                            headerOrigin = headerOrigin.Replace("http://http://", "http://");

                        response.Headers.Add("Access-Control-Allow-Origin", headerOrigin);
                    }
                    else
                        response.Headers.Add("Access-Control-Allow-Origin", "*");
                }
                else
                {
                    if (headers.AllKeys.Contains("Referer"))
                    {
                        string headerReferer = HttpContext.Current.Request.Headers.GetValues("Referer").FirstOrDefault();
                        if (!headerReferer.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                            headerReferer = "http://" + headerReferer;

                        response.Headers.Add("Access-Control-Allow-Origin", headerReferer);
                    }
                    else
                        response.Headers.Add("Access-Control-Allow-Origin", "*");
                }

                response.Headers.Add("Access-Control-Allow-Credentials", "true");
                // 去除
                response.Headers.Remove("Server");
                response.Headers.Remove("X-AspNet-Version");
                response.Headers.Remove("X-Powered-By");
                response.Headers.Remove("Server");
            }
        }
    }
}